package com.woniuxy.mini;

import com.woniuxy.core.service.ArticleLikeService;
import com.woniuxy.core.service.LikeService;
import com.woniuxy.entity.DTO.request.LikeRequest;
import com.woniuxy.util.ResponseData;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 饶志
 * @version 1.0
 * @since 2025-07-15  10:25
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/mini/like")
public class LikeController {

    private final LikeService likeService;

    @Autowired
    private ArticleLikeService articleLikeService;


    /**
     * 点赞/取消点赞接口
     *
     * @param userId     用户ID
     * @param targetId   目标ID（帖子/评论ID）
     * @param targetType 目标类型（1-帖子，2-评论）
     * @return 最新点赞数
     */
    @PostMapping("/toggle")
    public ResponseData<Long> toggleLike(@RequestBody LikeRequest likeRequest) {
        Long newCount = likeService.toggleLike(
                likeRequest.getUserId(),
                likeRequest.getTargetId(),
                likeRequest.getTargetType());
        return ResponseData.ok(newCount);
    }
    /**
     * 查询目标的点赞数
     *
     * @param targetId   目标ID（帖子/评论ID）
     * @param targetType 目标类型（1-帖子，2-评论）
     * @return 点赞数
     */
    @GetMapping("/count")
    public ResponseData<Long> getLikeCount(@RequestParam Long targetId, @RequestParam Integer targetType) {
        Long count = articleLikeService.countLikesByTargetIdAndType(targetId, targetType);
        return ResponseData.ok(count);
    }
    /**
     * 查询帖子的点赞数
     *
     * @param articleId 帖子ID
     * @return 点赞数
     */
    @GetMapping("/count/article")
    public ResponseData<Long> getArticleLikeCount(@RequestParam("articleId") Long articleId) {
        Long count = articleLikeService.countLikesByArticleId(articleId);
        return ResponseData.ok(count);
    }

}
